package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.management.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色管理控制器
 *
 * @Author:Mr.m
 * @Date: 2025-10-07 1:25
 * @Description: TODO
 */
@Slf4j
@RestController
@RequestMapping("/roles")
public class RoleController {
    @Autowired
    private RoleService roleService;

    //三大步骤   ① 拉架子   ② 补全参数和返回值  ③  代码

    /**
     * 查询所有角色数据
     * @return 角色列表响应数据
     */
    @GetMapping("/list")
    public Result list() {
        List<Role> roles = roleService.list();
        return Result.success(roles);
    }

    //根据id删除角色
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        log.info("删除角色id:{}",id);
        roleService.deleteById(id);
        return Result.success();
    }

    //修改角色
    @PutMapping
    public Result update(@RequestBody Role role){
        log.info("修改角色:{}",role);
        roleService.update(role);
        return Result.success();
    }

    //根据id查询角色
    @GetMapping("/{id}")
    public Result queryRoleById(@PathVariable("id") Integer id) {
        log.info("查询角色id:{}",id);
        Role role = roleService.queryRoleById(id);
        return Result.success(role);
    }

    //根据页面条件查询
    @GetMapping
    public Result queryRoleByPage(
            @RequestParam(name = "name", required = false) String name,
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        log.info("查询角色,参数:name:{},page:{},pageSize:{}", name, page, pageSize);
        PageResult<Role> pageResult = roleService.page(name, page, pageSize);
        return Result.success(pageResult);
    }

    //新增角色
    @PostMapping
    public Result addRole(@RequestBody Role role) {
        log.info("新增角色:{}", role);
        roleService.addRole(role);
        return Result.success();
    }

}
